package com.moorror.codegenvel.common.select;

import com.google.common.collect.Sets;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.Presentation;
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
import com.intellij.openapi.actionSystem.ex.CustomComponentAction;
import com.intellij.openapi.project.DumbAware;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Set;
import java.util.function.Consumer;

/**
 * @Author jiyanghuang
 * @Date 2024/4/11 23:15
 */
public class MROneSelect extends AnAction implements CustomComponentAction, DumbAware {
    private Set<String> evalOptions = Sets.newHashSet();
    private ComboBoxAction outputComboBox;
    private JComboBox<String> jComboBox;
    private String preSelect;
    private Consumer<String> currentSelect;

    public MROneSelect(Set<String> options, String tips, Consumer<String> currentSelect) {
        this.currentSelect = currentSelect;
        jComboBox = new JComboBox();
        jComboBox.setToolTipText(tips);
        options.forEach((s) -> {
            jComboBox.addItem(s);
        });
        jComboBox.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (!StringUtils.isEmpty(preSelect)) {
                    if (preSelect.equals(e.getItem())) {
                        return;
                    }
                }
                preSelect = e.getItem().toString();
                if(null != currentSelect){
                    currentSelect.accept(e.getItem().toString());
                }
            }
        });
    }

    @Override
    public @NotNull JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
        return jComboBox;
    }

    @Override
    public void actionPerformed(@NotNull AnActionEvent e) {
        System.out.println("---->");
    }
}

